and generally just crunch an awful lot of numbers.
For example, if you type
f(y) = y^2 + 1
a = 3.45e-2
sin( π/f(2-a) )
into the parser, then it will respond with
0.6019925995,
which, of course, is just what you asked for. Simple, isn’t it?
Clicking on any of the tiny icons at the top of the NumberCrunch window will change the display to show (in left-to-right order)
• the algebraic expression parser,
• the object list,
• the graph window,
• the help window,
• the disk Input/Output menu,
• and the parameter settings.
All of these modes and disk IO menu items can also be accessed through the main NumberCrunch menu. Holding the command key and typing 1, 2, 3, 4, or 6 is another shorthand for flipping between the modes. Each one is described below. (The mode descriptions can also be reached by clicking the appropriate button in the Help Window.)
This program is completely free for non-commercial use. You are welcome to use it and give it away, but you may not sell it for a profit. Comments are welcome.
My address is PO Box 347
Marlboro, VT 05344
(I will mail you a copy of the program, sample files and a Hypercard manual if you send me $17. Of course, if you can read this, you already HAVE the program, so your $17 will mainly get you a copy of the manual and encourage me to write an array-oriented, extendable, fancier version.)
- - The Parse Window - - - - - - - - - -
This is the heart of NumberCrunch’s calculator. Essentially, you type an algebraic expression to the parser, hit Return or Enter, and the value of the expression is output. Variables and functions can be defined using "=", i.e.
a = 2/3
f(x,y) = x sin(y)
The syntax for expressions follows the conventions of most common programming languages, with the addition of implicit multiplication. The arithmetic operators are +, -, *, / or ÷, and ** or ^ for raising to a power. Any two variables, numbers, or what-ever without any operation are assumed to be multiplying each other. For example the expressions "3*x", "3x", "3 x", and "3(x)" are all equal to "three times the value of x".
Numbers are expressed in the standard SANE format. For example, 4, 2.3356, -.002, 1.2e-6, 10E12, and ∞ are all valid numbers. (Here “∞” just means a number larger than the largest SANE number, about 1e4932.) The grouping symbols |, (, {, [ and ], }, ), | are all recognized, as well as ! (factorial) and the common trig and log functions. A list of all known functions is given in the Object Window.
Variables are defined by lines like
a_variable = 5 + 8
after which occurances of the the string “a_variable” will have the value 13. Variable names are not case-sensitive, and may contain up to 35 characters. They cannot begin with a number, so implicit multiplication of variables can be parsed. (This means that '4x' is really '4*x', but 'x4' is a variable name.) Variables can be listed and deleted in the Object Window.
Functions are defined by typing things like
f(a,b,c) = g(a)*(b/c) + the_radius
after which “f(1,2,x)” is equivalent to “g(1)*(2/x) + the_radius”. Like variables, functions can be listed and deleted from the Object Window. The definition of a function can be seen in the Parser by typing just the name of the function, with no arguments, and hitting return.
The parser will try to interpret expression as implicit multiplication if possible. For example, '4(1+3)' will give 16, just like '4*(1+3)' and '(1+3)4'. This can be confusing when variables are used. In particular, 'a'(1+4) can give one of three results: 1) if 'a' is undefined, it gives an error; 2) if 'a' is the name of a function, the function is applied to 5; 3) if 'a' is a variable, the result is a*(1+4).
Expressions are parsed when the Return or Enter key is pressed, depending on the value of the settings. The current “line” (or the selected text, if some is selected) is evaluated and the value of the last expression is typed on the next line. Here a “line” is the text between carriage returns, so the normal Macintosh wrap at the edge of the window is ignored. If several lines are selected, each is evaluated in turn. Shift-Return or CapsLock-Return will not evaluate an expression. Note that unlike the standard Mac convention, selecting text and then hitting return will NOT delete the selected text. Moreover, if Return is hit when the cursor is in the middle of a line, the line will NOT be broken up. (Assuming Return is set to evaluate expressions in the Settings.)
All of the standard Cut, Copy, Paste, and Clear commands and their keyboard shortcuts work in the Parser Window.
- - - The Object Window - - - - - - - - - -
This window shows a list of all defined variables and functions. The legal primitive functions can also be displayed. Objects (other than the primitive functions) may be deleted by selecting them and then clicking on the delete button.
Initially, values for 'e' and 'Pi' are set. Either of these may be deleted or reset. Their values can always be recovered by 'e = exp(1)' and 'Pi = π'. The symbol 'π' (option-p) is always recognized.
- - - The Graphs Window - - - - - - - -
This mode is the most complicated of them all. Essentially, you just enter an expression in each of the 6 text windows ('y:', 'x:', and the 4 graph limits) and then click on the Plot button to draw a graph. The 'x:' text must be a variable name, or an error occurs. The plot will show the 'y:' expression evaluated as the 'x:' variable runs from Xmin to Xmax, which are found by evaluating the text in the limits. Any legal expression which evaluates to a constant is OK as a limit. For example, if 'x:' is 'x', 'y:' is 'sin(x)', the Y limits are '1' and '-1', and the X limits are '0' and '2π', then one cycle of the graph of sin(x) vs x will be plotted.
When the cursor is over the graph, it appears as a small circle. Holding down the button will then show the co-ordinates of the crosshair. These values are saved as the variables Xclick and Yclick. (The limits of the graph are also saved and are called Xmin, Xmax, Ymin, and Ymax.)
More than one function can be plotted by separating them with semi-colons. For example, if 'y:' is 'a + 4x ; sin(x)' , then the line 'a + 4*x' and 'sin(x)' will both be plotted. (Assuming that 'a' is defined. If not, you get an error.)
Typing Tab will cycle through the text fields. Shift-Tab will cycle in the other direction, and option-Tab will select all of the current field.
The graph limits may also be reset with the mouse. To do this, click on the small button on the left (with the arrows in it). Then either draw a rectangle and click on 'ReSize to Rectangle', or click somewhere to set the zoom point (the cross on the screen) and then zoom In or Out. Note that the ReSize to Rectangle button will exit back to the graph mode (as will the Cancel button), but the zoom In and Out do not. This allows you to zoom in (or out) several times in succession. Each zoom gives 8x magnification.
If the limits become so close together that they print out the same (i.e. '.834' for both the min and max of X), then the higher limit will print in the form 'Xmin + 1.2e-5'. Since Xmin is a defined variable, this is a legal expression for a limit.
When the size of the window is very small, the limit text may not fit in the fields provided. (For example, you may see X max set to '1.23e' or some such.) Simply make the window bigger to see all the text.
Since the graph is saved as a picture, changing the size of the window can make it look very rough. Just hit the Plot button to draw it again. It is always drawn by evaluating the 'y=' expression every 3 pixels.
---- The Disk Input/Output Menu ----
This pull down menu lets you save or get data in several different formats.
The 'Open…', 'Save', and 'Save As…' options all read and write 'Ndoc' files, which can contain text, variables and functions, and the graph. The dialog in Save As determines which of these are saved. When the file is opened, the text and objects (if any) will be appended to the current text and objects, and the graph will replace the current graph.
If some objects are selected in the Object Window, the Save As item will read Save Selected Objects As, and will only save those variables or functions.
The 'Save Text…' item will copy the Parser Window to a TEXT file.
This becomes 'Save Selected Text…' if a selection range is active.
The 'Insert Text…' choice will read a TEXT file, insert it at the selection point, and select it. To evaluate this text, you then type Return or Enter.
The 'Copy Graph' item will put a PICTure of the current graph into the clipboard, where it can be pasted into the Scrapbook for later use. The actual QuickDraw commands are copied, not a bit-map, so the graphs can look pretty nice on a laser printer.
The 'Save Defaults' button will write the current environment (as determined in the settings) to a file in the System Folder which is opened whenever the program starts up.
- - - The Settings Window - - - - - - - - - -
This modal dialog allows you to change the settings.
The most important is the Default stuff in the top left corner. NumberCrunch tries to read a 'NumberCrunch Defaults' file from the System Folder when it starts up. This file can contain settings, objects, text, graph parameters, and the window position. If the 'Save Now' button is pressed (or Save Defaults chosen from the I/O menu), then the default file will be written (or created) with the current environment. What will be put in (and later read at start-up) depends on the 'Parser', 'Graph', 'Objects', and 'Window Position' check-boxes.
If the 'Auto Save' box is checked, then the defaults will be saved whenever the program exits, thus providing a 'continuous memory' between sessions.
The check box labeled 'Blink Graph Click Point' will affect what happens when you click in the graph. If this is turned on, then a blinking '+' and its co-ordinates will remain in the graph after you let the mouse up.
The 'NumberCrunch Menu' item will put or take out a menu in the main menu bar. All of the items in this menu can be accessed from from the NumberCrunch window; therefore, it is a matter of preference whether this menu exists or not. I think a DA without a menu in the main menu bar is cleaner, but…
Any characters in the 'Prompt' box will be output in the parser window each time an expression is evaluated. They will also be ignored at the start of a line.
- - - Bugs - - - - - - - - - - - - - - - - - -
I've fixed most of the obvious ones, but:
• If there isn't enough memory to create an object or a window, NumberCrunch may not exit gracefully.
• If a file cannot be opened (because it has been left open by a system crash, for example) then the program won't open it, but it won't tell you why. Since most errors are reported in the Parser text, I haven't put in any alerts yet (which is where this information should go).
• I've occasionally seen some conflicts between the host program and the NumberCrunch DA. In particular, the cursor may flicker or not be the right shape.
• The default file NumberCrunch in the system folder isn't treated exactly the same as the other save files. (The window position is saved, for example.) Occasionally this can lead to problems, for example if you try to save a file named NumberCrunch in the system folder. (The program will create such a file - you don't have to.)
• For other problems that come up - well, what did you expect from a free program???
Fixes :
--- from version 1.0:
- File I/0 system crashes
- Error on stuff after factorial, i.e. 4!/2
- Occasional crash after recursive function
--- from version 1.2
- Occasional crash on startup
(especially in system 6.0 and higher)
--- from version 1.3
- Crash in graph after closing during
blinking click point
--- from version 1.4
- added Int(x) (integer < x)
- and Sigma(x) ( 1 if x>0, 0 otherwise) functions
- - - More Rambling - - - - - - - - - - - - -
I wrote this program partly to see what it would be like to program a Mac, and partly because I didn't like any of the calculator-like programs I'd seen. I use the Mac mostly for numerical research in physics, and want my calculator to know the value of Boltzmann's constant and the speed of light. Now it does.
Programming on the Mac turned out to be hard. Not only because of the toolbox and umpty-ump manuals, but because working out a nice interface took much more time and effort than I'd expected.
I have plans to make this program into a real scientific computing environment, complete with arrays, matrices, contour plots, imaginary numbers, and hooks to call specialized routines written in Pascal or whatever. We'll see. If you're interested, write me.
- Jan '89
-----------------
UPDATE:
I'm still working on NumberCrunch II - the fancy version of NumberCrunch mentioned above. Look for it this soon I hope. I've already put it arrays, units, matrices, complex numbers, multiple windows and files (in the application), externally compiled routines, pascal-like functions and programs, and some little niceties like command-completion. I plan to sell that rather than giving it away, but it should only be $50 or so.
-----------------
Jim Mahoney
10/90
Oh, and by the way - try clicking somewhere in the help window besides in the buttons, if you haven't already.